#coding=utf-8
import requests
import F
import arcpy
import json
import time
arcpy.env.overwriteOutput = True
url = "https://restapi.amap.com/v5/place/polygon"
cnList = []
page_num = 1
while True:
    # params = {
    #     "polygon": "101.936302969606,30.9412228500545|103.392487760002,30.9412228500545|103.392487760002,28.851121545423|101.936302969606,28.851121545423",
    #     "keywords": "水泥".decode('utf-8'),
    #     "key" : "5ea734417b1c02abf874b11c6daf8b69",
    #     "type" : "170300",
    #     "page_num" : page_num,
    #     "page_size": 25,
    # }

    # params = {
    #     "polygon": "101.936302969606,30.9412228500545|103.392487760002,30.9412228500545|103.392487760002,28.851121545423|101.936302969606,28.851121545423",
    #     "key" : "5ea734417b1c02abf874b11c6daf8b69",
    #     "keywords": "加油站".decode('utf-8'),
    #     "type" : "010100",
    #     "page_num" : page_num,
    #     "page_size": 25,
    # }

    params = {
        "polygon": "101.936302969606,30.9412228500545|103.392487760002,30.9412228500545|103.392487760002,28.851121545423|101.936302969606,28.851121545423",
        "keywords": "保护区".decode('utf-8'),
        "key" : "5ea734417b1c02abf874b11c6daf8b69",
        "page_num" : page_num,
        "page_size": 25,
    }
    response = requests.get(url, params=params)
    data = json.loads(response.text)
    if not data['pois']:
        break

    dataCount = len(data['pois'])
    print dataCount,"当前页:%s"%page_num
    for i in range(dataCount):
        name = data['pois'][i]['name']
        county = data['pois'][i]['adname']
        address = data['pois'][i]['address']
        p_x = float(data['pois'][i]['location'].split(',')[0])
        p_y = float(data['pois'][i]['location'].split(',')[1])
        p_x_trans = F.gcj02_to_wgs84(p_x,p_y)[0]
        p_y_trans = F.gcj02_to_wgs84(p_x,p_y)[1]
        cnList.append( [p_x_trans,p_y_trans,name,county,address])
    page_num += 1
    time.sleep(4)
p = arcpy.CreateFeatureclass_management(r'e:\test','test2','POINT')
arcpy.AddField_management(p,'name','TEXT')
arcpy.AddField_management(p,'county','TEXT')
arcpy.AddField_management(p,'address','TEXT')
yb = arcpy.da.InsertCursor(p,['SHAPE@X','SHAPE@Y','name','county','address'])
for cn in cnList:
    yb.insertRow(cn)
del yb
